package com.yangff.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yangff.anno.LogExecuteTime;
import com.yangff.entity.product.entity.ProductComments;
import com.yangff.entity.product.entity.dto.ProductCommentsDTO;
import com.yangff.entity.product.entity.vo.ProductCommentsVo;
import com.yangff.entity.vo.ResultVo;
import com.yangff.service.IProductCommentsService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 产品评论表 前端控制器
 * </p>
 *
 * @author yangff
 * @since 2025-01-28
 */
@LogExecuteTime
@RestController
@RequiredArgsConstructor
@RequestMapping("/product-comments")
public class ProductCommentsController {

    private final IProductCommentsService productCommentsService;


    @GetMapping("/code/{code}")
    public List<ProductCommentsVo> getProductCommentsByCode(@PathVariable String code) {
        List<ProductComments> productComments = productCommentsService.list(new LambdaQueryWrapper<ProductComments>()
        .eq(ProductComments::getCode, code));

        return BeanUtil.copyToList(productComments, ProductCommentsVo.class);
    }


    @GetMapping("/user/{account}")
    public List<ProductCommentsVo> getProductCommentsByAccount(@PathVariable String account) {
        List<ProductComments> productComments = productCommentsService.list(new LambdaQueryWrapper<ProductComments>()
        .eq(ProductComments::getAccount, account));

        return BeanUtil.copyToList(productComments, ProductCommentsVo.class);
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping
    public ResultVo addProductComments(@RequestBody ProductCommentsDTO productCommentsDTO) {
        ProductComments productComments = BeanUtil.copyProperties(productCommentsDTO, ProductComments.class);
        productCommentsService.save(productComments);
        return ResultVo.success();
    }
}
